package com.danieltoms.gcSync.addressbook.screens;

import java.util.Enumeration;
import java.util.Vector;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;

import com.danieltoms.gcSync.GcSyncConstants;
import com.danieltoms.gcSync.addressbook.AddressBookExtension;
import com.danieltoms.gcSync.addressbook.resources.GcSyncAddressResResource;
import com.danieltoms.gcSync.addressbook.widgets.ClickableAddressLabelField;
import com.danieltoms.gcSync.addressbook.widgets.ClickableEmailLabelField;
import com.danieltoms.gcSync.addressbook.widgets.ClickablePhoneLabelField;
import com.danieltoms.gcSync.contact.BBContact;
import com.danieltoms.gcSync.google.GoogleDataItem;
import com.danieltoms.gcSync.google.GoogleTypedField;

public class ViewGmailInfo extends MainScreen
{
	BBContact _bbContact = null;
	GoogleDataItem _si = null;

	private MenuItem _editItem = new MenuItem(AddressBookExtension.getMessages().getMessage(GcSyncAddressResResource.MENU_EDITGMAIL), 100, 10)
	{
		public void run()
		{
			UiApplication.getUiApplication().pushScreen(new EditGmailInfo(_si, _bbContact));
		}
	};

	protected void makeMenu(Menu menu, int instance)
	{
		super.makeMenu(menu, instance);

		menu.add(_editItem);

		// menu.addSeparator();
		// menu.add(m_closeItem);

	}

	public boolean onClose()
	{

		close();
		return true;
	}

	public ViewGmailInfo(GoogleDataItem si, BBContact bbContact)
	{
		super(DEFAULT_CLOSE);

		_si = si;
		_bbContact = bbContact;
		drawScreen();
	}

	protected void onExposed()
	{
		drawScreen();

	}

	private void drawScreen()
	{
		// todo remove this and just keep the handle to the items
		// and update
		// them as appropriate
		this.deleteAll();
		// add title
		setTitle(new LabelField("View Gmail:" + _bbContact.getGivenName(), LabelField.ELLIPSIS
		        | LabelField.USE_ALL_WIDTH));

		if (_si != null)
		{

			this.add(new LabelField(_bbContact.getGivenName()));

			this.add(new LabelField(""));

			drawEmails();

			this.add(new LabelField(""));

			drawPhones();
			this.add(new LabelField(""));
			drawAddresses();

			this.add(new LabelField(""));
			drawIms();
		}
		else
			this.add(new RichTextField("Not synced ", Field.NON_FOCUSABLE));
	}

	private void drawEmails()
	{

		if (_si.getEmails() != null)
		{
			Enumeration emails = _si.getEmails().elements();

			if (emails != null && emails.hasMoreElements())
			{
				this.add(new LabelField("Email Addresses"));
				this.add(new SeparatorField());

				GoogleTypedField email = (GoogleTypedField) emails.nextElement();

				this.add(new ClickableEmailLabelField("Primary: " + email.getValue()));

				if (emails.hasMoreElements())
				{
					createEmailLabelField(GcSyncConstants.HOME_TYPE);
					createEmailLabelField(GcSyncConstants.WORK_TYPE);
					createEmailLabelField(GcSyncConstants.OTHER_TYPE);
				}

			}

		}
	}

	private void createEmailLabelField(String type)
	{
		Vector vector = _si.getTypedVector(_si.getEmails(), type);

		if (vector != null)
		{

			Enumeration emails = vector.elements();

			if (emails != null && emails.hasMoreElements())
			{
				GoogleTypedField email = (GoogleTypedField) emails.nextElement();

				if (email != null)
				{
					this.add(new LabelField(""));
					this.add(new LabelField(type));
					this.add(new ClickableEmailLabelField(email.getValue()));

					while (emails.hasMoreElements())
					{
						email = (GoogleTypedField) emails.nextElement();
						this.add(new ClickableEmailLabelField(email.getValue()));

					}
				}
			}
		}
	}

	private void drawPhones()
	{

		// this.add(new ClickableEmailLabelField("Primary: "
		// +_bbContact.getEmail()));

		if (_si.getPhones() != null)
		{
			this.add(new LabelField("Phone Numbers"));
			this.add(new SeparatorField());

			createPhoneLabelField(GcSyncConstants.HOME_TYPE);
			createPhoneLabelField(GcSyncConstants.WORK_TYPE);
			createPhoneLabelField(GcSyncConstants.MOBILE_TYPE);
			createPhoneLabelField(GcSyncConstants.HOME_FAX_TYPE);
			createPhoneLabelField(GcSyncConstants.WORK_FAX_TYPE);
			createPhoneLabelField(GcSyncConstants.PAGER_TYPE);
		}
	}

	private void createPhoneLabelField(String type)
	{

		Vector v = _si.getTypedVector(_si.getPhones(), type);

		if (v != null)
		{
			Enumeration phones = v.elements();

			if (phones != null && phones.hasMoreElements())
			{
				GoogleTypedField phone = (GoogleTypedField) phones.nextElement();

				if (phone != null)
				{
					this.add(new LabelField(""));
					this.add(new LabelField(type));
					this.add(new ClickablePhoneLabelField(phone.getValue()));

					while (phones.hasMoreElements())
					{
						phone = (GoogleTypedField) phones.nextElement();
						this.add(new ClickablePhoneLabelField(phone.getValue()));

					}
				}
			}
		}
	}

	private void drawAddresses()
	{

		// this.add(new ClickableEmailLabelField("Primary: "
		// +_bbContact.getEmail()));

		Vector v = _si.getAddresses();

		if (v != null)
		{
			this.add(new LabelField("Postal Addresses"));
			this.add(new SeparatorField());

			createAddressLabelField(GcSyncConstants.HOME_TYPE);
			createAddressLabelField(GcSyncConstants.WORK_TYPE);
			createAddressLabelField(GcSyncConstants.OTHER_TYPE);
		}
	}

	private void createAddressLabelField(String type)
	{
		Vector vector = _si.getTypedVector(_si.getAddresses(), type);

		if (vector != null)
		{
			Enumeration addresses = vector.elements();

			if (addresses != null && addresses.hasMoreElements())
			{
				GoogleTypedField address = (GoogleTypedField) addresses.nextElement();

				if (address != null)
				{
					this.add(new LabelField(""));
					this.add(new LabelField(type));
					this.add(new ClickableAddressLabelField(address.getValue()));

					while (addresses.hasMoreElements())
					{
						address = (GoogleTypedField) addresses.nextElement();
						this.add(new ClickableAddressLabelField(address.getValue()));

					}
				}
			}
		}
	}

	private void drawIms()
	{

		if (_si.getIms() != null)
		{
			this.add(new LabelField("Instant Messaging"));
			this.add(new SeparatorField());

			createIMLabelField(GcSyncConstants.IM_GOOGLE_TALK);
			createIMLabelField(GcSyncConstants.IM_AIM);
			createIMLabelField(GcSyncConstants.IM_YAHOO);
			createIMLabelField(GcSyncConstants.IM_MSN);
			createIMLabelField(GcSyncConstants.IM_ICQ);
			createIMLabelField(GcSyncConstants.IM_JABBER);

		}
	}

	private void createIMLabelField(String type)
	{
		Vector vector = _si.getTypedVector(_si.getIms(), type);

		if (vector != null)
		{

			Enumeration ims = vector.elements();

			if (ims != null && ims.hasMoreElements())
			{
				GoogleTypedField im = (GoogleTypedField) ims.nextElement();

				if (im != null)
				{
					this.add(new LabelField(""));
					this.add(new LabelField(type));
					this.add(new LabelField(im.getValue(), LabelField.FOCUSABLE));

					while (ims.hasMoreElements())
					{
						im = (GoogleTypedField) ims.nextElement();
						this.add(new LabelField(im.getValue()));

					}
				}
			}
		}
	}

}
